package com.ayz.gateway.center.infrastructure.application;

import com.ayz.gateway.center.infrastructure.notify.GatewayServiceChangeEventListener;
import com.ayz.gateway.common.notify.ApplicationEventMulticaster;
import com.ayz.gateway.common.notify.NotifyCenter;
import lombok.Getter;
import lombok.Setter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * @ClassName GatewayCenterStartListener
 * @Description TODO: 网关中心的启动事件
 * 监听应用程序上下文的刷新事件完成网关中心启动的设置
 * @Author 快乐的星球
 * @Date 2023/6/12 11:51
 * @Version 1.0
 **/
@Getter
@Setter
public class GatewayCenterStartListener implements ApplicationListener<ContextRefreshedEvent> {

    private static final Logger LOG=LoggerFactory.getLogger(GatewayCenterStartListener.class);

    private String ngxContainerName;

    public GatewayCenterStartListener(String ngxContainerName) {
        this.ngxContainerName = ngxContainerName;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        //1:注册 GatewayServiceChangeEventListener 网关服务变更事件监听器
        LOG.info("注册 GatewayServiceChangeEventListener 网关服务变更事件监听器!");
        ApplicationEventMulticaster eventMulticaster = NotifyCenter.getINSTANCE().getEventMulticaster();
        eventMulticaster.addApplicationListener(new GatewayServiceChangeEventListener(getNgxContainerName()));
    }
}
